import json

from flask import send_file, Blueprint, request, render_template
from .models import *

blue2 = Blueprint('fil', __name__)
import base64


def png_to_base64(png_file_path):
    with open(png_file_path, "rb") as image_file:
        # 读取PNG图像的二进制数据
        image_binary = image_file.read()

        # 使用base64编码将二进制数据转换为字符串
        base64_encoded = base64.b64encode(image_binary)

        # 将字节字符串解码为普通的UTF-8字符串
        base64_string = base64_encoded.decode("utf-8")

        return base64_string


@blue2.route('/pdf')
def pdf():
    # 替换为你的PDF文件路径
    print("ok")
    pdf_path = 'static/demo.pdf'
    tokens = request.headers.get('Authorization')
    print("tokens", tokens)
    # 使用send_file发送PDF文件
    return send_file(pdf_path, as_attachment=False)


@blue2.route('/download_pdf')
def download_pdf():
    # 替换为你的PDF文件路径
    pdf_path = 'static/demo.pdf'
    # 使用send_file发送PDF文件
    return send_file(pdf_path, as_attachment=True)


@blue2.route('/form')
def form():
    # 替换为你的PDF文件路径
    tokens = request.headers
    # print("tokens",tokens)
    param_value = request.args
    print("param_value", param_value)
    table_file_path = "App/static/routes/" + "table"
    with open(table_file_path, "w", encoding="utf-8") as f:
        f.write(str(param_value))
    # 使用send_file发送PDF文件
    return create_data("form")


@blue2.route('/', methods=['POST', "GET"])
def index():
    return render_template('index.html')


@blue2.route('/pdfs', methods=['POST', "GET"])
def pdfs():
    result = "okkk"
    pdf_path = 'App/static/demo.pdf'
    # 使用send_file发送PDF文件
    # with open(pdf_path,'rb') as f:
    #     print(f.read())
    #     pdf=f.read()
    # pdf=send_file(pdf_path, as_attachment=False)
    return create_data("pdf")


@blue2.route('/pdflists', methods=['POST', "GET"])
def pdflists():
    result = ""
    filenames = ["/pdfList.json", "/未审批.json","/已审批.json"]
    if request.args["whichList"]=="notYet":
        pdf_path = "App/static" + filenames[1]
    elif request.args["whichList"]=="already":
        pdf_path = "App/static" + filenames[2]
    else:
        pdf_path = "App/static" + filenames[0]
    with open(pdf_path, 'r', encoding="utf-8") as json_file:
        # 使用 json.load() 方法加载 JSON 数据
        pdflists = json.loads(json_file.read())
    return create_data(pdflists)


@blue2.route('/pdfAccept', methods=['POST', "GET"])
def pdfAccept():
    accept = 0
    print("args",request.args)
    filenames = ["/未审批.json","/已审批.json"]
    pdf_path1 = "App/static" + filenames[0]
    with open(pdf_path1, 'r', encoding="utf-8") as json_file:
        # 使用 json.load() 方法加载 JSON 数据
        notYet = json.loads(json_file.read())
    pdf_path2 = "App/static" + filenames[1]
    try:
        with open(pdf_path2, 'r', encoding="utf-8") as json_file:
        # 使用 json.load() 方法加载 JSON 数据
            already = json.loads(json_file.read())
    except:
        already=[]
    whichPdf=0
    for index,i in enumerate(notYet):
        if int(i["id"])==int(request.args["id"]):
            whichPdf=index
    already.append(notYet[whichPdf])
    del notYet[whichPdf]
    with open(pdf_path1, 'w') as json_file:
        json.dump(notYet, json_file)
    with open(pdf_path2, 'w') as json_file:
        json.dump(already, json_file)

    return create_data(accept)


@blue2.route('/pdfRefuse', methods=['POST', "GET"])
def pdfRefuse():
    refuse = 0
    return create_data(refuse)
